package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.impl.StudentServiceImpl;

import java.util.Scanner;

/*
*
*
* */
public abstract class BaseStudentController {

    StudentServiceImpl studentService = new StudentServiceImpl();
    Scanner sc = new Scanner(System.in);
    //开启学生管理系统,并展示菜单
    public final void start() {
        studentLoop:while (true){
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加");
                    addStudent();   //add
                    break;
                case "2":
                    //System.out.println("删除");
                    delectStudent();    //delect
                    break;
                case "3":
                    //System.out.println("修改");
                    updataStudent();    //updata
                    break;
                case "4":
                    //System.out.println("查询");
                    selectStudents();   //select
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统, 再见!");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    public final void updataStudent() {
        System.out.println("请输入id:");
        String id = sc.next();
        while (true){
            /*
             *  去业务层查找学号是否存在
             *   存在返回true 不存在返回false
             * */
            boolean flag = studentService.isExists(id);
            if (flag){
                break;
            }else {
                System.out.println("学号不存在,请重新输入");
                id =  sc.next();
            }
        }

        Student student = inputStudent(id);

        Student newStu = studentService.updataStudent(student);
        System.out.println("学号\t\t姓名\t年龄\t\t生日");

        System.out.println(newStu.getId() + "\t" + newStu.getName() + "\t" + newStu.getAge() + "\t\t" + newStu.getBirthday());
    }

    public final void delectStudent() {
        System.out.println("请输入id:");
        String id = sc.next();
        while (true){
            /*
             *  去业务层查找学号是否存在
             *   存在返回true 不存在返回false
             * */
            boolean flag = studentService.isExists(id);
            if (flag){
                boolean soult = studentService.delectStudent(id);
                System.out.println("删除成功!");
                break;
            }else {
                Student[] students = studentService.findAllStudent();
                if (students == null){
                    System.out.println("当前系统无数据,请先录入!!!");
                    return;
                }
                System.out.println("该学生信息不存在,请重新输入:");
                id =  sc.next();

            }
        }
    }

    public final void selectStudents() {
        System.out.println("请输入要查找的id:(输入\\all 查询全部)");
        String id = sc.next();
        if (id.equals("\\all")){
            Student[] students = studentService.findAllStudent();
            if (students != null) {
                System.out.println("学号\t\t姓名\t年龄\t\t生日");
                for (int i = 0; i < students.length; i++) {
                    Student stu = students[i];
                    if (stu != null) {
                        System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
                    }
                }
            }else {
                System.out.println("当前系统无数据,请先录入!!!");
            }
            return;
        }
        /*
         *  去业务层查找学号是否存在
         *   存在返回true 不存在返回false
         * */
        boolean flag = studentService.isExists(id);
        if (flag){
            Student stu = studentService.selectStudents(id);

            System.out.println("学号\t\t姓名\t年龄\t\t生日");
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "岁\t\t" + stu.getBirthday());
        }else {
            System.out.println("该学生信息不存在,请先录入:");
        }

    }

    public final void addStudent() {

        System.out.println("请输入id:");
        String id = sc.next();
        while (true){
            /*
            *  去业务层查找学号是否存在
            *   存在返回true 不存在返回false
            * */
            boolean flag = studentService.isExists(id);
            if (!flag){
                break;
            }else {
                System.out.println("学号已被占用,请重写输入");
                id =  sc.next();
            }
        }

        Student student = inputStudent(id);

        boolean result = studentService.addStudent(student);
        if (result == true){
            System.out.println("添加成功");
        }else
            System.out.println("添加失败");
    }

    //开闭原则: 对扩展的代码展开,对修改的代码关闭
    public abstract Student inputStudent(String id);

    public String inputStudentId(){
        String id = sc.next();
        while (true){
            /*
             *  去业务层查找学号是否存在
             *   存在返回true 不存在返回false
             * */
            boolean flag = studentService.isExists(id);
            if (!flag){
                break;
            }else {
                System.out.println("学号已被占用,请重新输入");
                id =  sc.next();
            }
        }
        return id;
    }

}
